<template>
  <div class="dialog">
    <el-dialog
      :title="'告警类别'+titleText"
      :visible.sync="showDialog"
      width="35%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        ref="ruleForm"
        label-position="left"
        :model="formData"
        class="el-form"
        :rules="rules"
      >
        <el-form-item label="类别名称:" class="w100p" prop="alarmTypeName">
          <el-input
            v-model="formData.alarmTypeName"
            clearable
          />
        </el-form-item>
        <el-form-item label="数据类型选择" prop="dataTypeList" class="w100p">
          <el-select
            v-model="formData.alarmTypeDataTypeMapperList"
            clearable
            multiple
            filterable
          >
            <el-option
              v-for="(item,index) in dataTypeList"
              :key="index"
              :value="item.dataTypeId"
              :label="item.dataTypeName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="描述:" class="w100p" prop="describe">
          <el-input
            v-model="formData.describe"
            clearable
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import alarmCategoryManagementApi from "@/api/monitoring-alarm/alarm-category-management";
// import historicalAlarmRecordApi from "@/api/monitoring-alarm/historical-alarm-record/index";

export default {
  name: '',
  components: { },
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {},
      rules: {
        alarmTypeName: [
          { required: true, message: "请输入告警类型名称", trigger: ["change", "blur"] }
        ],
        alarmTypeDataTypeMapperList: [
          { required: true, message: "请选择数据类型选择", trigger: ["change", "blur"] }
        ]
      },
      // 数据类型列表
      dataTypeList: [],
      showDialog: this.show
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {}
      }
      if (v === 'edit') {
        console.log( this.editObject);
        this.formData = this.editObject
        if (this.formData.alarmTypeDataTypeMapperList) {
          this.formData.alarmTypeDataTypeMapperList = this.formData.alarmTypeDataTypeMapperList.map(item => item.dataTypeId)
        }
      }
    }
  },
  created() {
    this.getselectlist()
  },
  mounted() {
  },
  methods: {
    changeDataType(data) {
      var index = this.dataTypeList.findIndex(a => a.dataTypeId === data)
      if (index !== -1) {
        return this.dataTypeList[index]
      }
    },
    // 获取下拉数据
    getselectlist() {
      // 获取所有数据类型
      alarmCategoryManagementApi.getalldatalist().then(
        res => {
          this.dataTypeList = res.data
        }
      )
    },
    save(formData) {
      this.$refs[formData].validate(async valid => {
        if (valid) {
          var params = {
            alarmTypeName: this.formData.alarmTypeName,
            describe: this.formData.describe
          }
          var dataTypeList = []
          this.formData.alarmTypeDataTypeMapperList.forEach(a => {
            var src = this.changeDataType(a)
            dataTypeList.push(src)
          });
          params.alarmTypeDataTypeMapperList = dataTypeList
          params.organizationNo = sessionStorage.organizationNo
          if (this.nowChanceStau === 'edit') {
            // 编辑接口
            params.alarmTypeNo = this.formData.alarmTypeNo
            params.alarmTypeId = this.formData.alarmTypeId
            alarmCategoryManagementApi.editlist(params).then(res => {
              if (res.code === 1) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.msg);
              }
            });
          } else if (this.nowChanceStau === 'add') {
            // const { data: res } = await alarmCategoryManagementApi.addlist(this.formData.alarmTypeName);
            // if (res === false) {
              alarmCategoryManagementApi.addlist(params).then(res => {
                console.log('---',params);
                console.log('===',res);
                if (res.code === 1) {
                  this.$message.success(res.msg);
                  this.close()
                } else {
                  this.$message.error(res.msg);
                }
              });
            // } else {
            //   this.$message.error("该类型名称已存在，请重新输入！")
            // }
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
